home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 1 / ETO Development Tools 1.iso / Tools - Objects / C++ / MPW C++ 3.1b1 / Examples / CPlusExamples / ShapesDocument.cp < prev    next >
Text File  |  1989-09-29  |  5KB  |  176 lines

  1. /*------------------------------------------------------------------------------
  2. #
  3. #    MultiFinder-Aware Simple Shapes Sample Application
  4. #
  5. #    CPlusShapesApp
  6. #
  7. #    This file: ShapesApp.cp - Implementation of the TShapesDocument Class
  8. #
  9. #    Copyright © 1988 Apple Computer, Inc.
  10. #    All rights reserved.
  11. #
  12. #    Versions:    1.0                 3/89
  13. #
  14. #    Components:
  15. #            CPlusShapesApp.make        March 1, 1989
  16. #            TApplicationCommon.h    March 1, 1989
  17. #            TApplication.h            March 1, 1989
  18. #            TDocument.h                March 1, 1989
  19. #            ShapesAppCommon.h        March 1, 1989
  20. #            ShapesApp.h                March 1, 1989
  21. #            ShapesDocument.h        March 1, 1989
  22. #            TApplication.cp            March 1, 1989
  23. #            TDocument.cp            March 1, 1989
  24. #            ShapesApp.cp            March 1, 1989
  25. #            ShapesDocument.cp        March 1, 1989
  26. #            TApplication.r            March 1, 1989
  27. #            ShapesApp.r                March 1, 1989
  28. #
  29. #   There are four main classes in this program. Each of
  30. #   these classes has a definition (.h) file and an
  31. #   implementation (.cp) file.  
  32. #   
  33. #   The TApplication class does all of the basic event
  34. #   handling and initialization necessary for Mac Toolbox
  35. #   applications. It maintains a list of TDocument objects,
  36. #   and passes events to the correct TDocument class when
  37. #   apropriate. 
  38. #   
  39. #   The TDocument class does all of the basic document
  40. #   handling work. TDocuments are objects that are
  41. #   associated with a window. Methods are provided to deal
  42. #   with update, activate, mouse-click, key down, and other
  43. #   events. Some additional classes which implement a
  44. #   linked list of TDocument objects are provided. 
  45. #   
  46. #   The TApplication and TDocument classes together define
  47. #   a basic framework for Mac applications, without having
  48. #   any specific knowledge about the type of data being
  49. #   displayed by the application's documents. They are a
  50. #   (very) crude implementation of the MacApp application
  51. #   model, without the sophisticated view heirarchies or
  52. #   any real error handling. 
  53. #   
  54. #   The TShapesApp class is a subclass of TApplication. It
  55. #   overrides several TApplication methods, including those
  56. #   for handling menu commands and cursor adjustment, and
  57. #   it does some necessary initialization.
  58. #   
  59. #   The TShapesDocument class is a subclass of TDocument. This
  60. #   class contains most of the special purpose code for
  61. #   shape drawing. In addition to overriding several of the
  62. #   TDocument methods, it defines a few additional
  63. #   methods which are used by the TShapesApp class to get
  64. #   information on the document state.  
  65. #
  66. #------------------------------------------------------------------------------*/
  67. // Mac Includes
  68. #include <Types.h>
  69. #include <QuickDraw.h>
  70. #include <Fonts.h>
  71. #include <Events.h>
  72. #include <Controls.h>
  73. #include <Windows.h>
  74. #include <Menus.h>
  75. #include <Dialogs.h>
  76. #include <Desk.h>
  77. #include <Scrap.h>
  78. #include <ToolUtils.h>
  79. #include <Memory.h>
  80. #include <SegLoad.h>
  81. #include <Files.h>
  82. #include <OSUtils.h>
  83. #include <Traps.h>
  84.  
  85. #include "ShapesDocument.h"
  86. #include "ShapesApp.h"
  87.  
  88. // kMinDocDim is used to limit the minimum dimension of a window when GrowWindow
  89. // is called.
  90. const short    kMinDocDim = 64;
  91.     
  92. // Define HiWrd and LoWrd macros for efficiency.
  93. #define HiWrd(aLong)    (((aLong) >> 16) & 0xFFFF)
  94. #define LoWrd(aLong)    ((aLong) & 0xFFFF)
  95.  
  96. // Define TopLeft and BotRight macros for convenience. Notice the implicit
  97. // dependency on the ordering of fields within a Rect
  98. #define TopLeft(aRect)    (* (Point *) &(aRect).top)
  99. #define BotRight(aRect)    (* (Point *) &(aRect).bottom)
  100.  
  101. // notice that we pass the resID parameter up to our base class,
  102. // which actually creates the window for us
  103. TShapesDocument::TShapesDocument(short resID)    : (resID)
  104. {
  105.     fShapeObj = nil;
  106.     
  107.     ShowWindow(fDocWindow);        // Make sure the window is visible
  108. }
  109.  
  110. // At this point, if there was a document associated with a
  111. // window, you could do any document saving processing if it is 'dirty'.
  112. // DoCloseWindow would return true if the window actually closed, i.e.,
  113. // the user didn’t cancel from a save dialog. This result is handy when
  114. // the user quits an application, but then cancels the save of a document
  115. // associated with a window.
  116.  
  117. TShapesDocument::~TShapesDocument(void)
  118. {
  119.     HideWindow(fDocWindow);
  120.     
  121.     if (fShapeObj) delete fShapeObj;
  122.     
  123.     // base class destructor will dispose of window
  124. }
  125.  
  126. void TShapesDocument::DoUpdate(void)
  127. {
  128.     BeginUpdate(fDocWindow);                // this sets up the visRgn 
  129.     if ( ! EmptyRgn(fDocWindow->visRgn) )    // draw if updating needs to be done 
  130.       {
  131.         DrawWindow();
  132.       }
  133.     EndUpdate(fDocWindow);
  134. }
  135.  
  136. // Draw the contents of an application window. 
  137.  
  138. void TShapesDocument::DrawWindow(void)
  139. {
  140.     SetPort(fDocWindow);
  141.     EraseRect(&fDocWindow->portRect);
  142.  
  143.     if (HasShape())
  144.         fShapeObj->Draw(qd.gray);
  145.  
  146. } // DrawWindow
  147.  
  148. // Change the type of shape displayed in the window
  149.  
  150. void TShapesDocument::ChangeShape(int newShape)
  151. {
  152.     Rect newRect = fDocWindow->portRect;
  153.     
  154.     SetPort(fDocWindow);
  155.  
  156.     if (HasShape()) {
  157.         fShapeObj->Erase();
  158.         delete fShapeObj;
  159.         fShapeObj = nil;
  160.     }
  161.     
  162.     switch (newShape) {
  163.         case iRRect:
  164.             fShapeObj = new TRoundRect(&newRect);
  165.             break;
  166.         case iOval:
  167.             fShapeObj = new TOval(&newRect);
  168.             break;
  169.         case iArc:
  170.             fShapeObj = new TArc(&newRect);
  171.             break;
  172.     }
  173.     
  174.     fShapeObj->Draw(qd.gray);
  175. }
  176.